from dealer import Dealer

class Table:
	def __init__(self, deck):
		u"Poker's table with cash and cards"
		self.pot = 0
		self.stake = 0
		self.players = []
		self.dealer = 0
		self.cards = []
		self.deck = deck
		self.phase = 0 # 0 - clean, 1 - preflop, 2 - flop, 3 - turn, 4 - river
		self.started = False #is game started?
		self.current_player = 0 #SHOULD PROBABLY BE self.StartingPlayer() but needs testing
	def Clear(self):
		self.pot = 0
		self.cards = []
		self.phase = 0
		self.stake = 0
		self.current_player = self.StartingPlayer() #EXPERIMENTAL
		self.started = False
	def Playable(self):
		if len(self.players) > 1:
			for i in range(len(self.players)):
				if self.players[i].ready == False:
					return False
			return True #at least 2 players and both ready
		return False
	def StartingPlayer(self):
		u"Make sure small blind starts always"
		for i in range(len(self.players)):
			if self.players[i].blind = 1:
				return i
	def ActivePlayers(self):
		act = 0
		for i in range(len(self.players)):
			if self.players[i].IsActive():
				act += 1
		return act
	def ResetAllActives(self):
		for i in range(len(self.players)):
			self.players[i].ResetBidActiveFlag()
	def Raise(self, amount):
		self.pot += amount 
		for i in range(len(self.players)):
			if self.players[i].table > self.stake:
				self.stake = self.players[i].table
		if self.phase:
			self.Broadcast("%s raises $%d, stake is now: %d" %(self.players[self.current_player].name, amount, self.stake))
	def Sit(self, player, uin, name):
		if len(self.players) > 7:
			raise UserWarning, "Too many players"
		if self.started:
			raise UserWarning, "Game already started, cannot sit"
		self.players.append(player)
		blind = 0
        	if self.players.index(player) == 0:
        		blind = 1
        	elif self.players.index(player) == 1:
        		blind = 2
        	player.SetIdentity(uin, name, blind)
	def TakeCards(self, count):
		cards = self.deck.Give(count)
		if count > 1:
			for i in range(len(cards)):
				self.cards.append(cards[i])
		else:
			cards = cards[0]
			self.cards.append(cards)
		self.Broadcast("New cards on the table: %s" % cards) 
	def Flop(self):
		self.TakeCards(3)
	def Turn(self):
                self.TakeCards(1)
	def River(self):
                self.TakeCards(1)
	def ResetPlayersBet(self):
		for i in range(len(self.players)):
			self.players[i].ResetBet()
	def DealPlayers(self):
		for i in range(len(self.players)):
			cards = self.deck.Give(2)
			self.players[i].TakeCards(cards)
			self.players[i].Text("Your hand: %s" % cards)
	def GiveAllToWinner(self, player):
		player.Earn(self.pot)
		self.Broadcast("%s wins! (with pot:$%d)" % (player.name, self.pot))
		self.ResetGame()
	def GiveCutToWinners(self, winners):
		cut = self.pot / len(winners)
		for i in range(len(winners)):
			winners[i].Earn(cut)
		self.Broadcast("DRAW: both players gets $%d" % cut)
		self.ResetGame()
	def ChangeBlinds(self):
		u"""There is next round so let's move blinds (lets swap because its only 2 players)
		  it is nasty ;/ good only for 2 players"""
		for i in range(len(self.players)):
			if self.players[i].blind == 1:
				self.players[i].blind = 2
			elif self.players[i].blind == 2:
				self.players[i].blind = 1
		Dealer().RetrieveBlinds(self)
	def ResetGame(self):
		self.Clear()
		self.ResetPlayersBet()
		self.ChangeBlinds()
	def NextPhase(self):
		if not self.Playable():
			raise UserWarning, "Game is not playable"
		if self.phase == 0:
			self.Broadcast("NEW GAME STARTS")
			self.DealPlayers()
			self.phase += 1
		elif self.phase == 1:
			self.Flop()
			self.phase += 1
		elif self.phase == 2:
			self.Turn()
			self.phase += 1
		elif self.phase == 3:
			self.River()
			self.phase += 1
		elif self.phase == 4:
			"Check Scores"
			winners = Dealer().Judge(self.players, self.cards)
			if winners[0] == winners[1]:
				self.GiveCutToWinners(winners)
				self.Broadcast("it's a DRAW, both players win with %s" % winners[0].set_name)
			else:
				self.GiveAllToWinner(winners[0])
				self.Broadcast("%s wins! with %s (%s)" % (winners[0].name, winners[0].strongest_hand, winners[0].set_name))
			#A POTEM WYCZYSCIC WSZYSTKO TRZEBA!!
		#self.current_player = 0 #moze nie jest to potrzebne
		self.ResetAllActives()
	def Broadcast(self, text):
		for i in range(len(self.players)):
			self.players[i].Text(text)






